package org.bigjava.buff.common.upload;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileUpload {

    private static String filePath = "E:\\tomcat\\tomcat7\\upload\\";

    public static String upload(MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            //存储图片的路径
            String originalFilename = file.getOriginalFilename();
            //获取文件后缀名称
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            //新图片名称
            String fileName = UUID.randomUUID().toString().replace("-", "") + suffix;
            //上传图片
            File newFile = new File(filePath + fileName);
            file.transferTo(newFile);
            //返回图片名称
            return fileName;
        }
        return null;
    }

}
